rm(list = setdiff(ls(), lsf.str())) 
#load data
data <- read.dta13("Study 1/Original Data/shp09_p_user.dta")
#select variables
data <- data[,which(colnames(data)%in%c("idint", "p09p19", "plingu09", "p09c60", "p09c61", "p09c62", "p09c63", "p09c64", "p09c65", "p09c66", "p09c67", "p09c68", "p09c69", "sex09", "age09", "isced09", "i09ptotn", "p09p10", "p09p15", "p09p17"))]

#Vote-intention SVP in 2009
data$vote_svp <- ifelse(as.numeric(data$p09p19)==9,1,
                        ifelse(as.numeric(data$p09p19)==3,NA,0))


data$vote_svp_other_not<-ifelse(as.numeric(data$p09p19)==9,1,
                               ifelse(as.numeric(data$p09p19)==3,NA, 
                                    ifelse(as.numeric(data$p09p19)==25 | as.numeric(data$p09p19)==26,3,2)))

data$vote_svp_other_not<-as.factor(data$vote_svp_other_not)
levels(data$vote_svp_other_not) <- c("Populist","Other parties","Not")
data$vote_svp_other_not <- relevel(data$vote_svp_other_not, ref = "Populist")

#language
data$language[as.numeric(data$plingu09)==6]=1
data$language[as.numeric(data$plingu09)==7]=2
data$language[as.numeric(data$plingu09)==8]=3

#Conscientiousness
data$c2_lazy <- data$p09c67
data$c2_lazy<-car::recode(data$c2_lazy,"-3=NA; -2=NA; -1=NA")
data$c2_lazy<-10-data$c2_lazy
data$c1_efficient<-data$p09c62
data$c1_efficient<-car::recode(data$c1_efficient,"-3=NA; -2=NA; -1=NA")
data$con <- rowMeans(mapply(zero1,with(data,data.frame(c2_lazy,c1_efficient))),na.rm=T)

#Extraversion
data$e1_reserved<-data$p09c60
data$e1_reserved<-car::recode(data$e1_reserved,"-3=NA; -2=NA; -1=NA")
data$e1_reserved<-10-data$e1_reserved

data$e1_sociable<-data$p09c65
data$e1_sociable<-car::recode(data$e1_sociable,"-3=NA; -2=NA; -1=NA")
data$ext <- rowMeans(mapply(zero1,with(data,data.frame(e1_sociable,e1_reserved))),na.rm=T)

#*Agreeableness
data$a1_trust<-data$p09c61
data$a1_trust<-car::recode(data$a1_trust,"-3=NA; -2=NA; -1=NA")

data$a2_fault<-data$p09c66
data$a2_fault<-car::recode(data$a2_fault,"-3=NA; -2=NA; -1=NA")
data$a2_fault<-10-data$a2_fault
data$agre <- rowMeans(mapply(zero1,with(data,data.frame(a2_fault,a1_trust))),na.rm=T)

#Openness
data$o1_imagination<-data$p09c64
data$o1_imagination<-car::recode(data$o1_imagination,"-3=NA; -2=NA; -1=NA")

data$o1_artistic<-data$p09c69
data$o1_artistic<-car::recode(data$o1_artistic,"-3=NA; -2=NA; -1=NA")
data$open <- rowMeans(mapply(zero1,with(data,data.frame(o1_artistic,o1_imagination))),na.rm=T)

#*Neuroticism
data$n1_nervous<-data$p09c68
data$n1_nervous<-car::recode(data$n1_nervous,"-3=NA; -2=NA; -1=NA")

data$n1_relaxed<- data$p09c63
data$n1_relaxed<-car::recode(data$n1_relaxed,"-3=NA; -2=NA; -1=NA")
data$n1_relaxed<-10-data$n1_relaxed
data$neu <- rowMeans(mapply(zero1,with(data,data.frame(n1_relaxed,n1_nervous))),na.rm=T)

#female
data$female<-car::recode(as.numeric(data$sex09),"6=0; 7=1")

#age
data$age<-car::recode(data$age09,"-2=NA; -1=NA; 0=NA; 1=NA; 2=NA; 3=NA; 4=NA; 5=NA; 6=NA; 7=NA; 8=NA; 9=NA; 10=NA; 11=NA; 12=NA; 13=NA; 14=NA; 15=NA; 16=NA; 17=NA")

#education
data$education<-as.numeric(data$isced09)
data$education<-car::recode(data$education,"1=NA; 2=NA; 3=NA")
data$education<-data$education-3

#income
data$income<-car::recode(data$i09ptotn, "-8=NA; -4=NA; -3=NA; -2=NA; -1=NA")

data$income_cat<-ifelse(data$income<=2000, 1,
                       ifelse(data$income> 2000 & data$income<10000, 2,
                              ifelse(data$income> 10000 & data$income<20000, 3,
                                     ifelse(data$income> 20000 & data$income<30000, 4,
                                            ifelse(data$income> 30000 & data$income<40000, 5,
                                                   ifelse(data$income> 40000 & data$income<50000, 6,
                                                          ifelse(data$income> 50000 & data$income<60000, 7,
                                                                 ifelse(data$income> 60000 & data$income<70000, 8,
                                                                        ifelse(data$income> 70000 & data$income<80000, 9,
                                                                               ifelse(data$income> 80000 & data$income<90000,10, 
                                                                                      ifelse(data$income> 90000 & data$income<100000,11,
                                                                                             ifelse(data$income> 100000 & data$income<150000,12, 
                                                                                                    ifelse(data$income> 150000,13, NA)))))))))))))

data$income<-zero1(data$income_cat)
data$income[is.na(data$income)==TRUE]=2
#income missing
data$income_mis<-ifelse(data$income==2,1,0)

#left-right
data$lr_placement<-data$p09p10
data$lr_placement<-car::recode(data$lr_placement,"-5=NA; -4=NA; -3=NA; -2=NA; -1=NA")

#anti-immigrant
data$anti_immi <- as.numeric(data$p09p15)
data$anti_immi<-car::recode(data$anti_immi,"3=NA; 4=NA; 5=NA")
data$anti_immi<-data$anti_immi-6

#econ cons
data$econ_cons <- as.numeric(data$p09p17)
data$econ_cons<-car::recode(data$econ_cons,"3=NA; 4=NA; 5=NA")
data$econ_cons<-data$econ_cons-6

data<-data[complete.cases(data$vote_svp), ]

data$language1<-ifelse(data$language==1,1,0)
data$language2<-ifelse(data$language==2,1,0)
data$language3<-ifelse(data$language==3,1,0)

data$edu1<-ifelse(data$education==2,1,0)
data$edu2<-ifelse(data$education==3,1,0)
data$edu3<-ifelse(data$education==4,1,0)
data$edu4<-ifelse(data$education==5,1,0)
data$edu5<-ifelse(data$education==6,1,0)
data$edu6<-ifelse(data$education==7,1,0)
data$edu7<-ifelse(data$education==8,1,0)
data$edu8<-ifelse(data$education==9,1,0)
data$edu9<-ifelse(data$education==10,1,0)

save(data, file="Study 1/Altered Data/Study1_Swiss_Household09.RData")



